// @ts-ignore
import emitter from "~/utils/mitt";
// Generate and append emoji icons
export const generateEmoji = (e:any) => {
    const emojiContainer = document.createElement('div');
    emojiContainer.classList.add('emoji-icons');
    for (let i = 128512; i <= 128591; i++) {
        const emojiCode = String.fromCodePoint(i);
        const emojiIcon = document.createElement('i');
        emojiIcon.classList.add('emoji-icon');
        emojiIcon.innerHTML = emojiCode;
        emojiContainer.appendChild(emojiIcon);
    }
    for (let i = 129306; i <= 129327; i++) {
        const emojiCode = String.fromCodePoint(i);
        const emojiIcon = document.createElement('i');
        emojiIcon.classList.add('emoji-icon');
        emojiIcon.innerHTML = emojiCode;
        emojiContainer.appendChild(emojiIcon);
    }
    // @ts-ignore
    document.getElementById(e).appendChild(emojiContainer);
}

export const toggleEmoji = () => {
    // @ts-ignore
    document.querySelector('.emoji-icons').classList.toggle('show');
}

export const emojiEvent = (event: any) => {
    if (event.target.classList.contains('emoji-icon')) {
        const emoji = event.target.textContent;
        const commentTextarea: any = document.querySelector('textarea[name="comment-father"]');
        const start = commentTextarea.selectionStart;
        const end = commentTextarea.selectionEnd;
        const currentValue = commentTextarea.value;
        commentTextarea.value = currentValue.substring(0, start) + emoji + currentValue.substring(end);
        commentTextarea.setSelectionRange(start + emoji.length, start + emoji.length);
        // @ts-ignore
        document.querySelector('.emoji-icons').classList.remove('show');
        emitter.emit('emoji-father')
    }
}

export const emojiEventReply = (event: any) => {
    if (event.target.classList.contains('emoji-icon')) {
        const emoji = event.target.textContent;
        const commentTextarea: any = document.querySelector('textarea[name="comment-reply"]');
        const start = commentTextarea.selectionStart;
        const end = commentTextarea.selectionEnd;
        const currentValue = commentTextarea.value;
        commentTextarea.value = currentValue.substring(0, start) + emoji + currentValue.substring(end);
        commentTextarea.setSelectionRange(start + emoji.length, start + emoji.length);
        // @ts-ignore
        document.querySelector('.emoji-icons').classList.remove('show');
        emitter.emit('emoji-reply')
    }
}
